from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager


class BaseModel(models.Model):
    id = models.BigAutoField(primary_key=True, help_text="Id", verbose_name="Id")
    create_time = models.DateTimeField(auto_now_add=True, null=True, blank=True, help_text="创建时间", verbose_name="创建时间")
    is_deleted = models.BooleanField(default=False, verbose_name='删除状态')

    class Meta:
        abstract = True
        verbose_name = '基础模型'
        verbose_name_plural = verbose_name


class UserManager(BaseUserManager):
    def _create_user(self, user_name, password, **kwargs):
        if not user_name:
            raise ValueError("请输入用户名")
        if not password:
            raise ValueError("请输入密码")

        user = self.model(user_name=user_name, **kwargs)
        user.set_password(password)
        user.save()
        return user

    def create_user(self, user_name, password, **kwargs):
        return self._create_user(user_name, password, **kwargs)

    def create_superuser(self, user_name, password, **kwargs):
        return self._create_user(user_name, password, **kwargs)


class User(AbstractBaseUser, BaseModel):
    user_name = models.CharField(
        unique=True, max_length=32, db_index=True, verbose_name="用户名"
    )
    email = models.EmailField(
        unique=True, max_length=64, db_index=True, verbose_name="邮箱"
    )
    password = models.CharField(max_length=128, verbose_name="密码")
    mobile_phone = models.CharField(unique=True, max_length=128, verbose_name="电话号码")
    IDENTITY_CHOICES = (
        (0, '求职者'),
        (1, '管理员'),
        (2, '招聘者'),
    )
    identity = models.IntegerField(choices=IDENTITY_CHOICES, null=True, blank=True)
    # 用于auth认证
    is_active = models.BooleanField(default=True)
    USERNAME_FIELD = "user_name"
    REQUIRED_FIELDS = ["username"]
    objects = UserManager()

    class Meta:
        db_table = "cloud_hr_user"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
        ordering = ("-create_time",)

    def get_full_name(self):
        return self.user_name

    def get_short_name(self):
        return self.user_name

    def __str__(self):
        return self.user_name


class Seeker(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    Name = models.CharField(max_length=10, verbose_name="姓名")
    Sex = models.CharField(max_length=10, verbose_name="性别")
    school = models.CharField(max_length=10, verbose_name="毕业学校")
    money = models.CharField(max_length=10, verbose_name="期望薪资")
    job = models.CharField(max_length=10, verbose_name="求职岗位")

    class Meta:
        db_table = "seeker"


class Recruiter(models.Model):
    # 用户表中与求职表的1对多关系 其中要identity=2才能发布职位
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    job_name = models.CharField(max_length=128, verbose_name="职位", null=False)
    city = models.CharField(max_length=128, verbose_name="城市", null=False)
    salary = models.CharField(max_length=128, verbose_name="薪资", default="3000")
    detail = models.TextField(verbose_name="职位介绍")
    detail1 = models.TextField(verbose_name="职位介绍1")
    education = models.CharField(max_length=128, verbose_name="学历")

    class Meta:
        db_table = "recruiter"


#定义中间表将职位表Recruiter与Seeker的多对多关系分开
class ResumeJob(models.Model):
    # 表中会是 table_id
    recruiter = models.ForeignKey(Recruiter, on_delete=models.CASCADE)
    seeker = models.ForeignKey(Seeker, on_delete=models.CASCADE)

    # TODO 增加其他字段 最好是两个表的共同字段
